import os
import cv2
import sys
import PySide6
from PySide6.QtWidgets import QFileDialog, QApplication
from Edge import Edge
from LSD import LSD
from Binary import Binary

# author: 周张珉
# 测试程序

dirname = os.path.dirname(PySide6.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path

if __name__ == "__main__":
    # 文件选择器
    app = QApplication(sys.argv)
    file_path = QFileDialog.getOpenFileName(filter="图像文件(*.png *.jpg *.jpeg)")
    print(file_path[0])
    if not os.path.exists(file_path[0]):
        print("文件不存在")
        os._exit(1)
    input_pic = cv2.imread(file_path[0])

    # 处理
    bin = Binary.binary_pic(input_pic)
    edge = Edge.edge_detector(bin)
    lsd = LSD.LSD(input_pic)
    output_pic = cv2.addWeighted(edge, 0.5, lsd, 0.5, 100)

    # 展示原始图片和处理后的图片
    cv2.imshow("Source", input_pic)
    cv2.imshow("Result", output_pic)
    cv2.waitKey(0)

    # 资源释放
    cv2.destroyAllWindows()
    app.exit()
